
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.jdbc.RowMapperFactory;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.constant.UserConstant;
import com.jf.cloud.service.sysmanager.dao.IPubgMatchMsgService;
import com.jf.cloud.service.sysmanager.entity.PubgMatch;
import com.jf.cloud.service.sysmanager.entity.PubgMatchMsg;
import com.jf.cloud.service.sysmanager.entity.PubgTeam;
import com.jf.cloud.service.sysmanager.entity.PubgUser;
import com.jf.cloud.service.sysmanager.entity.PubgUserMatch;


/**
 * 员工服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgMatchMsgService")
public class PubgMatchMsgServiceImpl extends BaseService<PubgMatchMsg, Long> implements IPubgMatchMsgService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgMatchMsgServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	/**
	 * 插入赛事消息
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 下午1:13:28
	 */
	@Override
	@Transactional
	public boolean insertMatchMsg(PubgUser pubgUser, Long matchId, String sendContent) {
		boolean boo = false;
		try {
			PubgMatchMsg pubgMatchMsg = new PubgMatchMsg();
			pubgMatchMsg.setSendTime(DateUtil.getCurrentDateTime());
			pubgMatchMsg.setSendContent(sendContent);
			
			pubgMatchMsg.setUserId(pubgUser.getRowId());
			pubgMatchMsg.setUserAccount(pubgUser.getUserAccount());
			pubgMatchMsg.setUserNickname(pubgUser.getNickname());
			pubgMatchMsg.setUserRole(pubgUser.getUserRole());
			
			// 查询赛事
			String sql = "SELECT ROW_ID, MATCH_NAME, CLUB_ID, CLUB_NAME FROM pubg_match WHERE ROW_ID = ? ";
			PubgMatch pubgMatch = this.baseDao.findObject(sql, RowMapperFactory.newInstance(PubgMatch.class), matchId);
			pubgMatchMsg.setMatchId(matchId);
			pubgMatchMsg.setMatchName(pubgMatch.getMatchName());
			pubgMatchMsg.setClubId(pubgMatch.getClubId());
			pubgMatchMsg.setClubName(pubgMatch.getClubName());
			
			// 普通队员发生消息时，查询俱乐部、战队信息 （2018.04.26 暂时队员不能发送消息）
			if (UserConstant.USER_ROLE_10.getId().equals(pubgUser.getUserRole())) {
				sql = "SELECT CLUB_ID, CLUB_NAME, TEAM_ID, TEMA_NAME FROM pubg_user_match ";
				sql += " WHERE MATCH_ID = ? AND USER_ID = ? ";
				PubgUserMatch pubgUserMatch = this.baseDao.findObject(sql, 
						RowMapperFactory.newInstance(PubgUserMatch.class), matchId, pubgUser.getRowId());
				if (pubgUserMatch == null) {
					log.error("未查询到用户参加的赛事。userId:" + pubgUser.getRowId() + "， matchId:" + matchId);
					return boo;
				}
				pubgMatchMsg.setTeamId(pubgUserMatch.getTeamId());
				pubgMatchMsg.setTeamName(pubgUserMatch.getTeamName());
			
			// 队长发送消息时，需要查询战队信息
			} else if (UserConstant.USER_ROLE_20.getId().equals(pubgUser.getUserRole())) {
				sql = "SELECT ROW_ID, TEAM_NAME FROM pubg_team WHERE CAPTAIN_ID LIKE '%" + pubgUser.getRowId() + ",%'";
				PubgTeam pubgTeam = this.baseDao.findObject(sql, RowMapperFactory.newInstance(PubgTeam.class));
				pubgMatchMsg.setTeamId(pubgTeam.getRowId());
				pubgMatchMsg.setTeamName(pubgTeam.getTeamName());
			}
			this.baseDao.saveAll(pubgMatchMsg);
			boo = true;
		} catch (Exception e) {
			log.error("插入赛事消息出错！", e);
		}	
		return boo;
	}

	/**
	 * 获取消息列表
	 * 
	 * @author 朱建文 创建时间：2018年4月26日 下午2:53:48
	 */
	public List<Map<String, Object>> getMatchMsgList(Long matchId) {
		List<Map<String, Object>> dataList = new ArrayList<Map<String,Object>>();
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" SELECT ");
			sql.append("   USER_NICKNAME nickname, USER_ROLE userRole, ");
			sql.append("   SEND_CONTENT content, SEND_TIME time ");
			sql.append(" FROM pubg_match_msg ");
			sql.append(" WHERE MATCH_ID = ? ");
			sql.append(" ORDER BY SEND_TIME DESC ");
			dataList = this.baseDao.findMapList(sql.toString(), matchId);
		} catch (DataAccessException e) {
			log.error("获取消息列表出错！", e);
		}
		return dataList;
	}
}
